import { Body, Controller, Delete, Get, Param, ParseArrayPipe, ParseIntPipe, Post, Put, Query, Req, Res } from '@nestjs/common';
import { ParseIntArrayPipe } from '@admin/common/pipe/parse-int-array.pipe';
import Result from '@admin/common/result/Result';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { RequirePermission } from '@admin/common/decorator/require-premission.decorator';
import { nowDateTime } from '@admin/common/utils';
import { JpSurnameService } from './service/jp-surname.service';
import { QueryJpSurnameDto, CreateJpSurnameDto, UpdateJpSurnameDto } from './dto/index';
import { Response } from 'express';
import { JpSurname } from '@prismaClient';
import { TableDataInfo } from '@admin/common/domain/TableDataInfo';
@ApiTags('姓氏管理')
@ApiBearerAuth()
@Controller('web/surname')
export class JpSurnameController {
  constructor(private surnameService: JpSurnameService) {}
  @ApiOperation({ summary: '查询姓氏管理列表' })
  @ApiQuery({ type: QueryJpSurnameDto })
  @ApiResponse({ type: TableDataInfo<JpSurname> })
  @RequirePermission('web:surname:list')
  @Get('/list')
  async listSurname(@Query() q: QueryJpSurnameDto): Promise<TableDataInfo<JpSurname>> {
    return Result.TableData(await this.surnameService.selectSurnameList(q));
  }
  @ApiOperation({ summary: '查询姓氏管理所有' })
  @ApiResponse({ type: Result<JpSurname[]> })
  @RequirePermission('web:surname:list')
  @Get('/data')
  async listSurnameData(): Promise<Result<JpSurname[]>> {
    return Result.ok(await this.surnameService.selectSurnameAll());
  }
  @ApiOperation({ summary: '导出姓氏管理xlsx文件' })
  @RequirePermission('web:surname:export')
  @Get('/export')
  async export(@Res() res: Response): Promise<void> {
    return this.surnameService.exportSurname(res);
  }
  @ApiOperation({ summary: '查询姓氏管理详细' })
  @ApiResponse({ type: Result<JpSurname> })
  @RequirePermission('web:surname:list')
  @Get('/:id')
  async getSurname(@Param('id', ParseIntPipe) id: number): Promise<Result<JpSurname>> {
    return Result.ok(await this.surnameService.selectSurnameById(id));
  }
  @ApiOperation({ summary: '新增姓氏管理' })
  @ApiResponse({ type: Result<JpSurname> })
  @ApiBody({ type: CreateJpSurnameDto })
  @RequirePermission('web:surname:add')
  @Post('/')
  async addSurname(@Body() jpSurname: CreateJpSurnameDto, @Req() req): Promise<Result<JpSurname>> {
    jpSurname['createTime'] = nowDateTime();
    jpSurname['updateTime'] = nowDateTime();
    return Result.ok(await this.surnameService.addSurname(jpSurname));
  }
  @ApiOperation({ summary: '修改姓氏管理' })
  @ApiResponse({ type: Result<any> })
  @ApiBody({ type: UpdateJpSurnameDto })
  @RequirePermission('web:surname:edit')
  @Put('/')
  async updateSurname(@Body() jpSurname: UpdateJpSurnameDto, @Req() req): Promise<Result<any>> {
    jpSurname['updateTime'] = nowDateTime();
    await this.surnameService.updateSurname(jpSurname);
    return Result.ok('修改成功！');
  }
  @ApiOperation({ summary: '删除姓氏管理' })
  @ApiResponse({ type: Result<any> })
  @RequirePermission('web:surname:remove')
  @Delete('/:ids')
  async delSurname(@Param('ids', ParseIntArrayPipe) ids: number[]): Promise<Result<any>> {
    const { count } = await this.surnameService.deleteSurnameByIds(ids);
    return Result.toAjax(count);
  }
}
